package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerIdearrdActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.IdearrdActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResIdearlist;
import com.kucai.yifaqapp.presenter.activity.IdearrdActivityPresenter;
import com.kucai.yifaqapp.ui.view.IdearrdActivityView;
import com.kucai.yifaqapp.util.DateSelector;
import com.kucai.yifaqapp.util.OnItemClickListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import butterknife.OnClick;

public class IdearrdActivity extends BaseActivity<IdearrdActivityPresenter> implements IdearrdActivityView {

    TextView titleLeftTv;
    TextView headerTitle;
    TextView alllist;
    TextView datalist;
    RecyclerView idearrdlist;
    LinearLayout skinbg;
    private Adapter adapter;
    private TimePickerView pvTime;
    private OptionsPickerView gamebtStrig1;//底部选择器
    private static final List<String> options1 = new ArrayList<>();//类型弹框
    private static final List<String> options2 = new ArrayList<>();//时间弹框
    private List<ResIdearlist.DataBean.FeedbackBean> listdata = new ArrayList<>();//时间弹框
    private String time;
    private String endtime;
    private String status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        alllist = findViewById(R.id.alllist);
        datalist = findViewById(R.id.datalist);
        idearrdlist = findViewById(R.id.idearrdlist);
        skinbg = findViewById(R.id.skinbg);

        headerTitle.setText("反馈记录");
        titleLeftTv.setText("返回");

        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        Date curDate = new Date(System.currentTimeMillis());
        String str = formatter.format(curDate);
        endtime = str;


        mPresenter.getidearlist(Info.user.token, null, null, null);
        titleLeftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gamebtStrig1 = new OptionsPickerView.Builder(IdearrdActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                if (options1 == 0) {
                    status = null;
                    alllist.setText("全部状态");
                    mPresenter.getidearlist(Info.user.token, null, time, endtime);
                } else if (options1 == 1) {
                    alllist.setText("待反馈");
                    status = "1";
                    mPresenter.getidearlist(Info.user.token, status, time, endtime);
                } else if (options1 == 2) {
                    alllist.setText("已反馈");
                    status = "2";
                    mPresenter.getidearlist(Info.user.token, status, time, endtime);
                }

            }


        }).setSubCalSize(20)//确定和取消文字大小
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTextColorCenter(Color.BLACK)//设置选中项的颜色
                .build();
        options1.clear();
        options1.add("全部状态");
        options1.add("待反馈");
        options1.add("已反馈");
        gamebtStrig1.setPicker(options1);
        options2.clear();
        //时间选择器
        pvTime = DateSelector.getPvDatePreDay(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                datalist.setText(DateSelector.getDateString(date));
                time = DateSelector.getDateString2(date);
                mPresenter.getidearlist(Info.user.token, status, time, endtime);
            }
        });
        idearrdlist.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
        idearrdlist.setAdapter(adapter);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_idearrecording;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    protected void initDagger() {
        DaggerIdearrdActivityComponent.builder()
                .idearrdActivityModule(new IdearrdActivityModule(this))
                .build().inject(this);
    }

    @OnClick({R.id.alllist, R.id.datalist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.alllist:
                gamebtStrig1.show();
                break;
            case R.id.datalist:
                pvTime.show();
                break;
        }
    }

    @Override
    public void getlist(ResIdearlist resIdearlist) {
        listdata.clear();
        listdata.addAll(resIdearlist.data.feedback);
        adapter.notifyDataSetChanged();
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(IdearrdActivity.this)
                    .inflate(R.layout.idearlist, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.leixing.setText(listdata.get(position).typeName);
            holder.zhaungtai.setText(listdata.get(position).statusName);
            holder.neirong.setText(listdata.get(position).content);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(IdearrdActivity.this, EditActivity.class);
                    intent.putExtra("Idearrd", listdata.get(position).id);
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return listdata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView leixing, zhaungtai, neirong;

            MViewHolder(View itemView) {
                super(itemView);
                leixing = (TextView) itemView.findViewById(R.id.leixing);
                zhaungtai = (TextView) itemView.findViewById(R.id.zhuangtai);
                neirong = (TextView) itemView.findViewById(R.id.neirong);


            }
        }
    }


}

